use crate::ai_agent_plat;

use super::{
    model::{AddAiAgentVo, AiAgent, ListAiAgentQuery, UpdateAiAgentVo},
    service,
};
use axum::response::IntoResponse;
use axum::{
    extract::{Extension, Path, Query, State},
    Json,
};
use common::{auth::Permission, error::AppError, page::TableDataInfo, response::AjaxResult};
use framework::state::AppState;
use serde_json::json;
use std::sync::Arc;
use tracing::info;

use framework::jwt::ClaimsData;

use super::super::utils::http_utils;

pub async fn list(State(state): State<Arc<AppState>>, Query(params): Query<ListAiAgentQuery>) -> Result<Json<TableDataInfo<AiAgent>>, AppError> {
    info!(
        "[HANDLER] Entering ai_agent::list with params: {:?}",
        params
    );

    // 调用 service 获取部门列表，这部分逻辑不变
    let list_data = service::select_ai_agent_list(&state.db_pool, params).await?;
    Ok(Json(list_data))
}

pub async fn get_detail(State(state): State<Arc<AppState>>, Path(ai_agent_id): Path<u64>) -> Result<impl IntoResponse, AppError> {
    // 1. 返回值类型改为更通用的 impl IntoResponse
    // 打印参数值和类型（关键排查日志）
    tracing::info!(
        "查询 ID: {}, 类型: {}",
        ai_agent_id,
        std::any::type_name::<u64>()
    );
    let ai_agent_plat = service::select_ai_agent_by_id(&state.db_pool, ai_agent_id).await?;

    // 2. 手动构建前端需要的、带有 "data" 键的 JSON 结构
    let response = json!({
        "code": 200,
        "msg": "操作成功",
        "data": ai_agent_plat // 将查询到的 ai_agent_plat 对象作为 "data" 字段的值
    });

    // 3. 将构建好的 JSON 返回
    Ok(Json(response))
}

pub async fn delete(State(state): State<Arc<AppState>>, Path(ai_agent_plat_id): Path<u64>) -> Result<Json<AjaxResult<()>>, AppError> {
    service::delete_ai_agent_by_id(&state.db_pool, ai_agent_plat_id).await?;
    Ok(Json(AjaxResult::<()>::success_msg("删除成功")))
}
/// 新增
pub async fn add(State(state): State<Arc<AppState>>, Extension(claims): Extension<ClaimsData>, Json(body): Json<AddAiAgentVo>) -> Result<Json<AjaxResult<()>>, AppError> {
    service::add_ai_agent(&state.db_pool, body, &claims.user_name).await?;
    Ok(Json(AjaxResult::<()>::success_msg("新增成功")))
}

/// 修改
pub async fn update(State(state): State<Arc<AppState>>, Extension(claims): Extension<ClaimsData>, Json(body): Json<UpdateAiAgentVo>) -> Result<Json<AjaxResult<()>>, AppError> {
    service::update_ai_agent(&state.db_pool, body, &claims.user_name).await?;
    Ok(Json(AjaxResult::<()>::success_msg("修改成功")))
}

/// 获取部门详细信息
pub async fn get_ai_agent_bot_list(State(state): State<Arc<AppState>>, Path(ai_agent_plat_id): Path<u64>) -> Result<impl IntoResponse, AppError> {
    // 1. 返回值类型改为更通用的 impl IntoResponse

    let result = service::get_ai_agent_bot_list(&state.db_pool, ai_agent_plat_id).await?;

    // 2. 手动构建前端需要的、带有 "data" 键的 JSON 结构
    let response = json!({
        "code": 200,
        "msg": "操作成功",
        "data": result // 将查询到的 ai_agent_plat 对象作为 "data" 字段的值
    });

    // 3. 将构建好的 JSON 返回
    Ok(Json(response))
}

pub async fn get_check_ai_agent_sta(State(state): State<Arc<AppState>>, Path(ai_agent_id): Path<u64>) -> Result<Json<AjaxResult<()>>, AppError> {
    // 1. 返回值类型改为更通用的 impl IntoResponse

    let result = service::get_check_ai_agent_sta(&state.db_pool, ai_agent_id).await?;
    Ok(Json(AjaxResult::<()>::success_msg("修改成功")))
    // 3. 将构建好的 JSON 返回
}
